package com.smiier.skin.ui;

import java.util.ArrayList;

import com.smiier.skin.constant.Constant;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.smiier.skin.utils.DBUtil;
import com.smiier.skin.vo.TipMessageVo;

import cn.o.app.ui.OBadgeView;
import cn.skinapp.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class PersonalDoctorFragment extends BasicFragment {
	
	
	private OBadgeView badge;
	protected View mBadgeTarget;
	
	private static boolean  isShow = false;
	

	public PersonalDoctorFragment(Context context) {
		super(context);
	}

	public PersonalDoctorFragment(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PersonalDoctorFragment(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}
	
	public void setBageViewTarget(View v) {
		mBadgeTarget = v;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		PersonalDoctorView doctorView = new PersonalDoctorView(getContext());
		doctorView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));

		badge = new OBadgeView(getContext(), mBadgeTarget);

		badge.setBackgroundResource(R.drawable.no_read_point);

		badge.setBadgeMargin(CommonUtility.dip2px(getContext(), 20),
				CommonUtility.dip2px(getContext(), 5));

		badge.setHeight(1);
		badge.setWidth(1);
		
		
		
		IntentFilter noReadTab = new IntentFilter(
				Constant.RECIVER_MYIFNO_TAB_NOREAD);
		getContext().registerReceiver(noRead, noReadTab);
		
		if(GlobalSettings.sUser != null
				|| GlobalSettings.sToken != null){
			initView();
		}
		
		setContentView(doctorView);
	}
	
	
	private void initView() {
		DBUtil dbUtil = null;
		if (dbUtil == null) {
			dbUtil = DBUtil.getInstance(getContext());
		}
		
		
		/*Intent intent = new Intent();
		intent.setAction(Constant.RECIVER_MYIFNO_TAB_NOREAD);
		intent.putExtra("chat", true);
		getContext().sendBroadcast(intent);*/

		ArrayList<TipMessageVo> list = dbUtil.query(TipMessageVo.class);

		int noReadNumber = 0;

		if (!list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {

				if (list.get(i).readState == 0) {
					noReadNumber++;
				}
			}
		}
		
		
		Log.i("testbadgeview", "noReadNumber:" + noReadNumber);
		
		if (noReadNumber > 0) {
			if (!badge.isShown()) {
				badge.show();
			}
		}else{
			badge.hide();
		}

	}

	BroadcastReceiver noRead = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			
			boolean chating = intent.getBooleanExtra("chat", false);
			
			Log.i("testxxx", "Tab.noread:" + chating);
			
			Log.i("testxxx", "badge.isshow:" + (!badge.isShown()) );
			
			if(!badge.isShown()){
			
				if(chating){
					badge.show();
				}
			}else{
				
				if(!chating){
					badge.hide();
				}
			}
			

			boolean isShow = intent.getBooleanExtra("show", false);
			
			if(!badge.isShown()){
				if(isShow){
					badge.show();
				}
			}
			
			/*
			if (isShow) {
				badge.show();
			} else {
				badge.hide();
			}*/
		}
	};

	@Override
	public void onDestroy() {
		super.onDestroy();
		CommonUtility.unRegisteReciver((Activity) getContext(), noRead);
	}

}
